package com.clz.appdesign.myapp.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * @author chenliangzhi
 * @date 2018/5/14
 * @describe
 */

public class MyView extends View {


    public MyView(Context context) {
        super(context);
    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }



    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //画笔属性设置
        Paint paint = new Paint();
        //抗锯齿
        paint.setAntiAlias(true);
        //设置画笔颜色
        paint.setColor(Color.RED);
        //画笔样式
        paint.setStyle(Paint.Style.FILL);
        //画笔宽度
        paint.setStrokeWidth(5);
        //画笔阴影
        paint.setShadowLayer(10, 15, 15, Color.GREEN);
        //设置画布背景颜色
        canvas.drawRGB(255, 255, 255);
        //画圆
        //canvas.drawCircle(190, 200, 150, paint);

        //画点
        paint.setStrokeWidth(15);
        paint.setStyle(Paint.Style.FILL);
        //canvas.drawPoint(300, 300, paint);
        //画多条线,四个点，两两连成线
        float ponits[] = {10, 10, 100, 100, 200, 200, 400, 400};
        // canvas.drawLines(ponits,paint);
        //画多个点
        //offset-->跳过几个数值，count--->参与绘制的有几个数值
        // canvas.drawPoints(ponits, 2, 4, paint);

        //画矩形
        //canvas.drawRect(400,400,650,650,paint);

        RectF rect = new RectF(400, 400, 750, 650);
        //canvas.drawRect(rect, paint);
        //画圆角矩形
        // canvas.drawRoundRect(rect, 20, 20, paint);

        //画圆
        //canvas.drawCircle(800,800,100,paint);
        //画椭圆
        // canvas.drawOval(rect,paint);

        //画弧
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(5);
        /**
         * radius:阴影的倾斜度
         dx:水平位移
         dy:垂直位移
         */
        paint.setShadowLayer(0, 0, 0, Color.RED);
        RectF rect1 = new RectF(100, 10, 300, 100);
        /**
         * RectF oval:生成椭圆的矩形
         float startAngle：弧开始的角度，以X轴正方向为0度
         float sweepAngle：弧持续的角度
         boolean useCenter:是否有弧的两边，True，还两边，False，只有一条弧
         */
        //canvas.drawArc(rect1, 0, 90, true, paint);

        RectF rect2 = new RectF(400, 10, 600, 100);
        //canvas.drawArc(rect2, 0, 90, false, paint);
        //画直线路径
        paint = new Paint();
        paint.setColor(Color.GREEN);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(5);

        Path path = new Path();
        path.moveTo(10, 10);
        path.lineTo(10, 100);
        path.lineTo(300, 100);//画第二条直线
        path.lineTo(500, 100);
        // path.close();
        //canvas.drawPath(path, paint);
        //画矩形路径
        //第一个逆向生成
        Path ccwRectPth = new Path();
        RectF rectF1 = new RectF(50, 50, 240, 200);
        ccwRectPth.addRect(rectF1, Path.Direction.CCW);

        //第二个顺向生成
        Path cwPath = new Path();
        RectF rectF2 = new RectF(290, 50, 480, 200);
        cwPath.addRect(rectF2, Path.Direction.CW);
        //画出这两个路径
        //canvas.drawPath(ccwRectPth,paint);
        //canvas.drawPath(cwPath,paint);
        //根据路径写出文字
        String text = "风萧萧兮易水寒,壮士一去兮不复返";
        paint.setColor(Color.RED);
        paint.setTextSize(30);
        //逆向书写文字
        //canvas.drawTextOnPath(text,ccwRectPth,0,18,paint);
        //顺向书写文字
        //canvas.drawTextOnPath(text,cwPath,0,18,paint);

        //圆角矩形路径
        Path roundPath = new Path();
        roundPath.addRoundRect(rectF1, 10, 15, Path.Direction.CCW);
        //canvas.drawPath(roundPath,paint);

        //圆形路径
        Path circlePath = new Path();
        circlePath.addCircle(100, 200, 50, Path.Direction.CW);
        // canvas.drawPath(circlePath,paint);

        //椭圆路径
        Path ovalPath = new Path();
        ovalPath.addOval(rectF1, Path.Direction.CW);
        // canvas.drawPath(ovalPath, paint);
        //弧形路径
        Path arcPath = new Path();
        arcPath.addArc(rectF1, 0, 270);
        //canvas.drawPath(arcPath,paint);

        //文字样式设置及倾斜度正负区别
        Paint paint2 = new Paint();
        paint2.setColor(Color.BLACK);
        paint2.setStrokeWidth(5);
        //指定是否使用抗锯齿功能，如果使用，会使绘图速度变慢
        paint2.setAntiAlias(true);
        paint2.setStyle(Paint.Style.FILL);
        paint2.setTextSize(80);
        //字体样式设置
        //设置是否为粗体样式
        paint2.setFakeBoldText(true);
        //设置下划线
        paint2.setUnderlineText(true);
        //设置删除线
        paint2.setStrikeThruText(true);
        //设置字体水平倾斜度 -0.25 往右斜 0.25 往左斜
        paint2.setTextSkewX((float) -0.25);
        canvas.drawText("再见只是陌生人", 10, 100, paint2);
        paint2.setTextSkewX((float) 0.25);
        canvas.drawText("再见只是陌生人", 10, 200, paint2);
        //文字拉伸效果
        paint2.setTextScaleX(2);
        paint2.setColor(Color.RED);
        canvas.drawText("再见只是陌生人",0,200,paint2);
    }
}
